﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using weka.classifiers.meta;

using weka.classifiers.functions;

using weka.core;
using java.io;
using weka.clusterers;

namespace WekaSample
{
    class Program
    {

        static void Classify()
        {
            Instances dataSetForTrain = new Instances(new BufferedReader(
                new FileReader(@"C:\Weka-3-6\data\iris.arff")));
            dataSetForTrain.setClassIndex(dataSetForTrain.numAttributes() - 1);

            /* the following lines are using bagging-of-decision-trees as the training model
            string options = @"-P 100 -S 1 -I 10 -W weka.classifiers.trees.REPTree -- -M 2 -V 0.0010 -N 3 -S 1 -L -1";
            Bagging model = new Bagging();
            model.setOptions(options.Split(' '));
            model.buildClassifier(dataSetForTrain);
            */

            // using libsvm
            string options = @"-S 1 -C 1.0 -E 0.01 -B 1.0";
            LibSVM model = new LibSVM();
            model.setOptions(options.Split(' '));
            model.buildClassifier(dataSetForTrain);
            
            //
            /*
             * the following code shows how to save the model to the disk file
             * and then load it
             * [IMPORTANT] for reproducing the result and save training time
            if (System.IO.File.Exists(fileName))
            {
                System.Console.WriteLine("read from weka file,please wait...");
                model = (Bagging)weka.core.SerializationHelper.read(Properties.model);
            }
            else
            {
                System.Console.WriteLine("build model,please wait...");
                model.setOptions(options);
                model.buildClassifier(trainDataSet);
                SerializationHelper.write(Properties.model, bagging);
            } 
             */

            // test the instances from iris.arff
            Instances dataSetForTest = new Instances(new BufferedReader(
                new FileReader(@"C:\Weka-3-6\data\iris.arff")));

            dataSetForTest.setClassIndex(dataSetForTest.numAttributes() - 1);

            for (int i = 0; i < dataSetForTest.numInstances(); i++)
            {
                var inst = dataSetForTest.instance(i);
                int label = (int) model.classifyInstance(inst);
                System.Console.WriteLine(label);
            }
            
        }


        public static void Main(string[] args)
        {
            Classify();
        }
    }
}

